/*
 * Name     : MazewarNode.java
 * Author(s): Jill San Luis
 * Created  : Mar 14, 2011, 12:32:13 PM
 */
package mazewar.network;

import java.io.IOException;

import java.net.InetSocketAddress;
import java.net.ServerSocket;

/**
 * The MazewarNode class
 *
 * @author  Jill San Luis
 */
public class MazewarNode {

    private String userName;
    private InetSocketAddress multicastAddress;
    private InetSocketAddress localAddress;

//    private boolean firstNode = true;
    /**
     * Creates a new instance of MazewarNode.
     *
     * @param   userName  DOCUMENT ME!
     *
     * @throws  InstantiationException  DOCUMENT ME!
     */
    public MazewarNode(String userName) throws InstantiationException {
        this.userName = userName;
        this.multicastAddress = MazewarNodeConfig.getInstance().getMulticastAddress();
        this.localAddress = MazewarNodeConfig.getInstance().getNodeAddress();

        ServerSocket ss = null;

        try {
            ss = new ServerSocket(localAddress.getPort());
        } catch (IOException e) {
            throw new InstantiationException("Cannot find available ephemereal port in this host.");
        }
//
//        if (!firstNode) {
//
//            try {
//                ss = new ServerSocket(0);
//            } catch (IOException e) {
//                throw new InstantiationException("Cannot find available ephemereal port in this host.");
//            }
//        }
    }

    /**
     * Creates a new MazewarNode object.
     *
     * @param  info  DOCUMENT ME!
     */
    public MazewarNode(MazewarNode info) {
        this.userName = info.getUserName();
        this.multicastAddress = info.getMulticastAddress();
        this.localAddress = info.getLocalAddress();
    }

    /**
     * @return  the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @return  the multicastAddress
     */
    public InetSocketAddress getMulticastAddress() {
        return multicastAddress;
    }

    /**
     * @return  the localAddress
     */
    public InetSocketAddress getLocalAddress() {
        return localAddress;
    }
//    @Override
//    public int hashCode()
//    {
//        int retval = 0;
//        if ( zAddresses != null )
//            retval += zAddresses.hashCode();
//        if ( zPort != null )
//            retval += zPort.hashCode();
//        return retval;
//    }
}
